package com.simas.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
//import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;

/**
 * 
 * @author Administrator
 * Bila menggunakan gambar dan/atau text, maka UWidth dan UHeight diisi 0
 * Bila tidak menggunakan gambar dan text, maka UWidth dan UHeight harus diisi 
 *
 */
public class CellButton extends Field {
	private String label;
	private int focusedForegroundColor;
	private int focusedBackgroundColor;
	private Bitmap bitmapImage;
	private int intUPosX;
	private int intUPosY;
	private int intUHeight;
	private int intUWidth;
	
	public CellButton(String label, 					// label
						int focusedForegroundColor, 	// warna text, focused
						int focusedBackgroundColor, 	// warna background, focused
						long style, 					// style (tidak dipakai)
						Bitmap bitmapImage,				// image
						int intUPosX,					// posisi X (shade)
						int intUPosY,					// posisi Y (shade)
						int intUWidth,					// width (shade)
						int intUHeight) {				// height (shade)
		//super(style);
		this.label = label;
		this.focusedForegroundColor = focusedForegroundColor;
		this.focusedBackgroundColor = focusedBackgroundColor;
		this.bitmapImage = bitmapImage;
		
		int intDispWidth = Display.getWidth();
		double dblRatio = 1;
		if (intDispWidth < 480) {
			dblRatio = intDispWidth / 480d;
		}
		
		this.intUPosX = (int) (intUPosX * dblRatio);
		this.intUPosY = (int) (intUPosY * dblRatio);
		this.intUHeight = (int) (intUHeight * dblRatio);
		this.intUWidth = (int) (intUWidth * dblRatio);
	}
	
//	public String getLabel() {
//		return label;
//	}
	
	public int getPreferredHeight() {
		int intHeight = 0;
		if (intUHeight == 0) {
			if (bitmapImage != null) {
				if (bitmapImage.getHeight() > getFont().getHeight()) {
					intHeight = bitmapImage.getHeight() + 8;
				} else {
					intHeight = getFont().getHeight() + 8;
				}
			} else {
				intHeight = getFont().getHeight() + 8;
			}
		} else {
			intHeight = intUHeight;
		}
		return intHeight;
	}
	
	public int getPreferredWidth() {
		int intWidth = 0;
		if (intUWidth == 0) {
			if (bitmapImage != null) {
				intWidth = bitmapImage.getWidth() + getFont().getAdvance(label) + 8;
			} else {
				intWidth = getFont().getAdvance(label) + 8;
			}
		} else {
			intWidth = intUWidth;
		}
		return intWidth;
		
	}
	
	protected void layout(int width, int height) {
		setExtent(Math.min(width, getPreferredWidth()), Math.min(height, getPreferredHeight()));
	}
	
	protected void paint(Graphics graphics) {
		if (isFocus()) {
			if (bitmapImage != null) {
				// focus, ada image
				// posisi shade
				graphics.setColor(focusedBackgroundColor);
				graphics.setGlobalAlpha(100);
				int intFocusX = 0 + intUPosX;
				int intFocusY = 0 + intUPosY;
				int intFocusWidth = getPreferredWidth();
				int intFocusHeight = getPreferredHeight();
				graphics.fillRoundRect(intFocusX, intFocusY, intFocusWidth, intFocusHeight, 20, 20);
				
				// posisi image
				int imageX = 4;
				int imageY = 4;
				int imageWidth = bitmapImage.getWidth();
				int imageHeight = bitmapImage.getHeight();
				graphics.drawBitmap(imageX, imageY, imageWidth, imageHeight, bitmapImage, 0, 0);
				
				// posisi text
				graphics.setColor(focusedForegroundColor);
				graphics.setGlobalAlpha(255);
				int textX = bitmapImage.getWidth() + 8;
				int textY = (getHeight() - getFont().getHeight()) / 2;
				graphics.drawText(label, textX, textY);
				
			} else {
				// focus, tidak ada image
				// posisi shade
				graphics.setColor(focusedBackgroundColor);
				graphics.setGlobalAlpha(100);
				int intFocusX = 0 + intUPosX;;
				int intFocusY = 0 + intUPosY;
				int intFocusWidth = getPreferredWidth();
				int intFocusHeight = getPreferredHeight();
				graphics.fillRoundRect(intFocusX, intFocusY, intFocusWidth, intFocusHeight, 20, 20);
				
				graphics.setColor(focusedBackgroundColor);
				graphics.setGlobalAlpha(255);
				//graphics.drawLine(intFocusX,intFocusY,intFocusWidth,intFocusY);
				//graphics.drawLine(intFocusX,intFocusY,intFocusX,intFocusHeight);
				//graphics.drawLine(intFocusX,intFocusHeight-1,intFocusWidth,intFocusHeight-1);
				//graphics.drawLine(intFocusX,intFocusHeight-2,intFocusWidth,intFocusHeight-2);
				//graphics.drawLine(intFocusWidth-1,intFocusHeight-1,intFocusWidth-1,intFocusY);
				
				// posisi text
				graphics.setColor(focusedForegroundColor);
				graphics.setGlobalAlpha(255);
				graphics.drawText(label, 4, 4);
			}
		} else {
			// tidak memakai shade
			if (bitmapImage != null) {
				// tidak focus, ada image
				// posisi image
				int imageX = 4;
				int imageY = (getHeight() - bitmapImage.getHeight()) / 2;
				int imageWidth = bitmapImage.getWidth();
				int imageHeight = bitmapImage.getHeight();
				graphics.drawBitmap(imageX, imageY, imageWidth, imageHeight, bitmapImage, 0, 0);
				
				// posisi text
				int textX = bitmapImage.getWidth() + 8;
				int textY = (getHeight() - getFont().getHeight()) / 2;
				graphics.drawText(label, textX, textY);
			} else {
				// tidak focus, tidak ada image
				graphics.drawText(label, 4, 4);
			}
		}
	}
	
	public boolean isFocusable() {
		return true;
	}
	
	protected void drawFocus(Graphics graphics, boolean on) {
	}
	
	protected void onFocus(int direction) {
		super.onFocus(direction);
		invalidate();
	}
	
	protected void onUnfocus() {
		super.onUnfocus();
		invalidate();
	}
	
	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(0);
		return true;
	}
	
	protected boolean keyChar(char character, int status, int time) {
		if (character == Keypad.KEY_ENTER) {
			fieldChangeNotify(0);
			return true;
		}
		return super.keyChar(character, status, time);
	}
}
